{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "1f6f6cad",
   "metadata": {},
   "outputs": [],
   "source": [
    "import jieba\n",
    "import numpy as np\n",
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "af639c1b",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "D:\\envs\\Python\\Python37\\lib\\site-packages\\IPython\\core\\interactiveshell.py:3166: DtypeWarning: Columns (9) have mixed types.Specify dtype option on import or set low_memory=False.\n",
      "  interactivity=interactivity, compiler=compiler, result=result)\n"
     ]
    }
   ],
   "source": [
    "category_df = pd.read_csv('../datasets/淘宝_全4级分类.csv', encoding='utf-8-sig', header=0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "45dbe2c9",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>groupId</th>\n",
       "      <th>groupName</th>\n",
       "      <th>id1</th>\n",
       "      <th>name1</th>\n",
       "      <th>id2</th>\n",
       "      <th>name2</th>\n",
       "      <th>id3</th>\n",
       "      <th>name3</th>\n",
       "      <th>id4</th>\n",
       "      <th>name4</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>G_1</td>\n",
       "      <td>游戏话费</td>\n",
       "      <td>40</td>\n",
       "      <td>腾讯QQ专区</td>\n",
       "      <td>50007211</td>\n",
       "      <td>QQ游戏大厅道具</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>G_1</td>\n",
       "      <td>游戏话费</td>\n",
       "      <td>40</td>\n",
       "      <td>腾讯QQ专区</td>\n",
       "      <td>50007185</td>\n",
       "      <td>QQ音速</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>G_1</td>\n",
       "      <td>游戏话费</td>\n",
       "      <td>40</td>\n",
       "      <td>腾讯QQ专区</td>\n",
       "      <td>50005457</td>\n",
       "      <td>QQ秀</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>G_1</td>\n",
       "      <td>游戏话费</td>\n",
       "      <td>40</td>\n",
       "      <td>腾讯QQ专区</td>\n",
       "      <td>50005491</td>\n",
       "      <td>QQ其它</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>G_1</td>\n",
       "      <td>游戏话费</td>\n",
       "      <td>40</td>\n",
       "      <td>腾讯QQ专区</td>\n",
       "      <td>50005458</td>\n",
       "      <td>QQ宠物</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "  groupId groupName  id1   name1       id2     name2  id3 name3  id4 name4\n",
       "0     G_1      游戏话费   40  腾讯QQ专区  50007211  QQ游戏大厅道具  NaN   NaN  NaN   NaN\n",
       "1     G_1      游戏话费   40  腾讯QQ专区  50007185      QQ音速  NaN   NaN  NaN   NaN\n",
       "2     G_1      游戏话费   40  腾讯QQ专区  50005457       QQ秀  NaN   NaN  NaN   NaN\n",
       "3     G_1      游戏话费   40  腾讯QQ专区  50005491      QQ其它  NaN   NaN  NaN   NaN\n",
       "4     G_1      游戏话费   40  腾讯QQ专区  50005458      QQ宠物  NaN   NaN  NaN   NaN"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "category_df.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "3a94e275",
   "metadata": {},
   "outputs": [],
   "source": [
    "# categorys = category_df[['groupId', 'groupName', 'id1', 'name1', 'id2', 'name2']].astype('string').drop_duplicates()\n",
    "categorys = category_df.astype('string').drop_duplicates()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "9e526663",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>groupId</th>\n",
       "      <th>groupName</th>\n",
       "      <th>id1</th>\n",
       "      <th>name1</th>\n",
       "      <th>id2</th>\n",
       "      <th>name2</th>\n",
       "      <th>id3</th>\n",
       "      <th>name3</th>\n",
       "      <th>id4</th>\n",
       "      <th>name4</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>G_1</td>\n",
       "      <td>游戏话费</td>\n",
       "      <td>40</td>\n",
       "      <td>腾讯QQ专区</td>\n",
       "      <td>50007211</td>\n",
       "      <td>QQ游戏大厅道具</td>\n",
       "      <td>&lt;NA&gt;</td>\n",
       "      <td>&lt;NA&gt;</td>\n",
       "      <td>&lt;NA&gt;</td>\n",
       "      <td>&lt;NA&gt;</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>G_1</td>\n",
       "      <td>游戏话费</td>\n",
       "      <td>40</td>\n",
       "      <td>腾讯QQ专区</td>\n",
       "      <td>50007185</td>\n",
       "      <td>QQ音速</td>\n",
       "      <td>&lt;NA&gt;</td>\n",
       "      <td>&lt;NA&gt;</td>\n",
       "      <td>&lt;NA&gt;</td>\n",
       "      <td>&lt;NA&gt;</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>G_1</td>\n",
       "      <td>游戏话费</td>\n",
       "      <td>40</td>\n",
       "      <td>腾讯QQ专区</td>\n",
       "      <td>50005457</td>\n",
       "      <td>QQ秀</td>\n",
       "      <td>&lt;NA&gt;</td>\n",
       "      <td>&lt;NA&gt;</td>\n",
       "      <td>&lt;NA&gt;</td>\n",
       "      <td>&lt;NA&gt;</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>G_1</td>\n",
       "      <td>游戏话费</td>\n",
       "      <td>40</td>\n",
       "      <td>腾讯QQ专区</td>\n",
       "      <td>50005491</td>\n",
       "      <td>QQ其它</td>\n",
       "      <td>&lt;NA&gt;</td>\n",
       "      <td>&lt;NA&gt;</td>\n",
       "      <td>&lt;NA&gt;</td>\n",
       "      <td>&lt;NA&gt;</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>G_1</td>\n",
       "      <td>游戏话费</td>\n",
       "      <td>40</td>\n",
       "      <td>腾讯QQ专区</td>\n",
       "      <td>50005458</td>\n",
       "      <td>QQ宠物</td>\n",
       "      <td>&lt;NA&gt;</td>\n",
       "      <td>&lt;NA&gt;</td>\n",
       "      <td>&lt;NA&gt;</td>\n",
       "      <td>&lt;NA&gt;</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>572064</th>\n",
       "      <td>G_11</td>\n",
       "      <td>生活服务</td>\n",
       "      <td>50026555</td>\n",
       "      <td>购物提货券</td>\n",
       "      <td>201173506</td>\n",
       "      <td>多次品牌兑换卡</td>\n",
       "      <td>201306003.0</td>\n",
       "      <td>小家电兑换卡</td>\n",
       "      <td>201310104.0</td>\n",
       "      <td>厨房电器兑换卡</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>572065</th>\n",
       "      <td>G_11</td>\n",
       "      <td>生活服务</td>\n",
       "      <td>50026555</td>\n",
       "      <td>购物提货券</td>\n",
       "      <td>201173506</td>\n",
       "      <td>多次品牌兑换卡</td>\n",
       "      <td>201309904.0</td>\n",
       "      <td>3C数码兑换卡</td>\n",
       "      <td>201310406.0</td>\n",
       "      <td>电玩游戏兑换卡</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>572066</th>\n",
       "      <td>G_12</td>\n",
       "      <td>其他商品</td>\n",
       "      <td>201162107</td>\n",
       "      <td>汽车零部件/养护/美容/维保</td>\n",
       "      <td>50014479</td>\n",
       "      <td>美容/维修/汽保工具</td>\n",
       "      <td>50014511.0</td>\n",
       "      <td>汽车维修保养工具</td>\n",
       "      <td>201303107.0</td>\n",
       "      <td>废油抽接机</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>572067</th>\n",
       "      <td>G_12</td>\n",
       "      <td>其他商品</td>\n",
       "      <td>201162107</td>\n",
       "      <td>汽车零部件/养护/美容/维保</td>\n",
       "      <td>50014479</td>\n",
       "      <td>美容/维修/汽保工具</td>\n",
       "      <td>50014511.0</td>\n",
       "      <td>汽车维修保养工具</td>\n",
       "      <td>201304101.0</td>\n",
       "      <td>车底检查镜</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>572068</th>\n",
       "      <td>G_12</td>\n",
       "      <td>其他商品</td>\n",
       "      <td>201162107</td>\n",
       "      <td>汽车零部件/养护/美容/维保</td>\n",
       "      <td>50014479</td>\n",
       "      <td>美容/维修/汽保工具</td>\n",
       "      <td>50014511.0</td>\n",
       "      <td>汽车维修保养工具</td>\n",
       "      <td>201304201.0</td>\n",
       "      <td>修车躺板</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>572069 rows × 10 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "       groupId groupName        id1           name1        id2       name2  \\\n",
       "0          G_1      游戏话费         40          腾讯QQ专区   50007211    QQ游戏大厅道具   \n",
       "1          G_1      游戏话费         40          腾讯QQ专区   50007185        QQ音速   \n",
       "2          G_1      游戏话费         40          腾讯QQ专区   50005457         QQ秀   \n",
       "3          G_1      游戏话费         40          腾讯QQ专区   50005491        QQ其它   \n",
       "4          G_1      游戏话费         40          腾讯QQ专区   50005458        QQ宠物   \n",
       "...        ...       ...        ...             ...        ...         ...   \n",
       "572064    G_11      生活服务   50026555           购物提货券  201173506     多次品牌兑换卡   \n",
       "572065    G_11      生活服务   50026555           购物提货券  201173506     多次品牌兑换卡   \n",
       "572066    G_12      其他商品  201162107  汽车零部件/养护/美容/维保   50014479  美容/维修/汽保工具   \n",
       "572067    G_12      其他商品  201162107  汽车零部件/养护/美容/维保   50014479  美容/维修/汽保工具   \n",
       "572068    G_12      其他商品  201162107  汽车零部件/养护/美容/维保   50014479  美容/维修/汽保工具   \n",
       "\n",
       "                id3     name3          id4    name4  \n",
       "0              <NA>      <NA>         <NA>     <NA>  \n",
       "1              <NA>      <NA>         <NA>     <NA>  \n",
       "2              <NA>      <NA>         <NA>     <NA>  \n",
       "3              <NA>      <NA>         <NA>     <NA>  \n",
       "4              <NA>      <NA>         <NA>     <NA>  \n",
       "...             ...       ...          ...      ...  \n",
       "572064  201306003.0    小家电兑换卡  201310104.0  厨房电器兑换卡  \n",
       "572065  201309904.0   3C数码兑换卡  201310406.0  电玩游戏兑换卡  \n",
       "572066   50014511.0  汽车维修保养工具  201303107.0    废油抽接机  \n",
       "572067   50014511.0  汽车维修保养工具  201304101.0    车底检查镜  \n",
       "572068   50014511.0  汽车维修保养工具  201304201.0     修车躺板  \n",
       "\n",
       "[572069 rows x 10 columns]"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "categorys"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "id": "5922eb36",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 记录标签 原始信息\n",
    "label = []\n",
    "\n",
    "for row in categorys.itertuples():\n",
    "    label0 = [row[1] ,row[1]]\n",
    "    label1 = [row[1], row[3]]\n",
    "    label2 = [row[1], row[5]]\n",
    "    \n",
    "    label3 = []\n",
    "#     label4 = []\n",
    "    if not pd.isnull(row[7]):\n",
    "        label3 = [row[1], row[7]]\n",
    "#         if not pd.isnull(row[9]):\n",
    "#             label4 = [row[1], row[9]]\n",
    "    \n",
    "#     for i in [label0, label1, label2]:\n",
    "    for i in [label0, label1, label2, label3]:\n",
    "#     for i in [label0, label1, label2, label3, label4]:\n",
    "        if i not in label:\n",
    "            label.append(i)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "id": "44a5ded2",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "45014"
      ]
     },
     "execution_count": 38,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(label)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "id": "e77be4d6",
   "metadata": {},
   "outputs": [],
   "source": [
    "with open('../datasets/category.label.raw', 'w') as label_f:\n",
    "    for i in label:\n",
    "        if len(i) == 2:\n",
    "        # print(i[0], i[1])\n",
    "            label_f.write(i[0] + ',' + i[1] + '\\n')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "id": "8d1f4b1b",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 记录边 原始信息\n",
    "edge = []\n",
    "\n",
    "for row in categorys.itertuples():\n",
    "\n",
    "    edge0 = [row[1] ,row[3]]\n",
    "    edge1 = [row[3], row[5]]\n",
    "    \n",
    "    edge2 = []\n",
    "#     edge3 = []\n",
    "    if not pd.isnull(row[7]):\n",
    "        edge2 = [row[5], row[7]]\n",
    "#         if not pd.isnull(row[9]):\n",
    "#             edge3 = [row[7], row[9]]\n",
    "    \n",
    "#     for i in [edge0, edge1]:\n",
    "    for i in [edge0, edge1, edge2]:\n",
    "#     for i in [edge0, edge1, edge2, edge3]:\n",
    "        if i not in edge:\n",
    "            edge.append(i)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "id": "2d5b4456",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "70392"
      ]
     },
     "execution_count": 41,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(edge)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "id": "e15c35f3",
   "metadata": {},
   "outputs": [],
   "source": [
    "with open('../datasets/category.edge.raw', 'w') as edge_f:\n",
    "    for i in edge:\n",
    "        # print(i[0], i[1])\n",
    "        if len(i) == 2:\n",
    "            edge_f.write(i[0] + ',' + i[1] + '\\n')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "dd91b635",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
